ifndef CC
CC=gcc
endif

ifndef CFLAGS
CFLAGS = -O2 -Wall -g -Wundef -Wstrict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -Werror-implicit-function-declaration
endif

ifndef PKG_CONFIG
PKG_CONFIG = pkg-config
endif

SRC = src
INCLUDE = $(SRC)

FUNC=$(SRC)/func
MODULE=$(SRC)/module
OS=$(SRC)/os
UI=$(SRC)/ui
DRIVER=$(SRC)/driver

CFLAGS += -I$(INCLUDE)

-include defconfig

BINALL = wfs

ifeq ($(CONFIG_CLI), y)
BINALL += wfs_cli
endif

ALL = $(BINALL)

all: $(ALL)

#################################################
# top level
#################################################
OBJS  = $(SRC)/daemon.o
OBJS += $(SRC)/dispatcher.o
OBJS += $(SRC)/if_manager.o
OBJS += $(SRC)/log.o
OBJS += $(SRC)/loop.o
OBJS += $(SRC)/main.o
OBJS += $(SRC)/util.o
OBJS += $(SRC)/event.o

OBJS_cli  = $(UI)/cli.o
OBJS_cli += $(SRC)/log.o
OBJS_cli += $(SRC)/wfs_api.o
#end of top level

#################################################
# os
#################################################
ifeq ($(CONFIG_OS), linux)
CFLAGS += -DCONFIG_OS
OS_LINUX=$(OS)/linux
CFLAGS += -I$(OS_LINUX)
OBJS += $(OS_LINUX)/os.o
endif
#end of os

#################################################
# func
#################################################
FUNC_GENERIC=$(FUNC)/generic
CFLAGS += -I$(FUNC_GENERIC)
OBJS += $(FUNC_GENERIC)/func_generic.o

ifeq ($(CONFIG_STA), y)
CFLAGS += -DCONFIG_STA
FUNC_STA=$(FUNC)/sta
CFLAGS += -I$(FUNC_STA)
OBJS += $(FUNC_STA)/func_sta.o
endif
#end of func

#################################################
# module
#################################################
ifeq ($(CONFIG_STA), y)
MODULE_STA=$(MODULE)/sta
CFLAGS += -I$(MODULE_STA)
OBJS += $(MODULE_STA)/sta.o
endif

ifeq ($(CONFIG_WSC), y)
CFLAGS += -DCONFIG_WSC
MODULE_WSC=$(MODULE)/wsc
CFLAGS += -I$(MODULE_WSC)
OBJS += $(MODULE_WSC)/wsc.o
endif
#end of module

#################################################
# driver
#################################################
ifeq ($(CONFIG_NL80211), y)
CFLAGS += -DCONFIG_NL80211
NL3xFOUND := $(shell $(PKG_CONFIG) --atleast-version=3.2 libnl-3.0 && echo Y)
ifneq ($(NL3xFOUND), Y)
NL31FOUND := $(shell $(PKG_CONFIG) --exact-version=3.1 libnl-3.1 && echo Y)
ifneq ($(NL31FOUND), Y)
NL3FOUND := $(shell $(PKG_CONFIG) --atleast-version=3 libnl-3.0 && echo Y)
ifneq ($(NL3FOUND), Y)
NL2FOUND := $(shell $(PKG_CONFIG) --atleast-version=2 libnl-2.0 && echo Y)
ifneq ($(NL2FOUND), Y)
NL1FOUND := $(shell $(PKG_CONFIG) --atleast-version=1 libnl-1 && echo Y)
endif
endif
endif
endif
ifeq ($(NL1FOUND), Y)
NLLIBNAME = libnl-1
endif
ifeq ($(NL2FOUND), Y)
CFLAGS += -DCONFIG_LIBNL20
LIBS += -lnl-genl
NLLIBNAME = libnl-2.0
endif
ifeq ($(NL3xFOUND), Y)
# libnl 3.2 might be found as 3.2 and 3.0
NL3FOUND = N
CFLAGS += -DCONFIG_LIBNL30
LIBS += -lnl-genl-3
NLLIBNAME = libnl-3.0
endif
ifeq ($(NL3FOUND), Y)
CFLAGS += -DCONFIG_LIBNL30
LIBS += -lnl-genl
NLLIBNAME = libnl-3.0
endif
# nl-3.1 has a broken libnl-gnl-3.1.pc file
# as show by pkg-config --debug --libs --cflags --exact-version=3.1 libnl-genl-3.1;echo $?
ifeq ($(NL31FOUND), Y)
CFLAGS += -DCONFIG_LIBNL30
LIBS += -lnl-genl
NLLIBNAME = libnl-3.1
endif
ifeq ($(NLLIBNAME),)
$(error Cannot find development files for any supported version of libnl)
endif
LIBS += $(shell $(PKG_CONFIG) --libs $(NLLIBNAME))
CFLAGS += $(shell $(PKG_CONFIG) --cflags $(NLLIBNAME))
DRIVER_NL80211=$(DRIVER)/nl80211
CFLAGS += -I$(DRIVER_NL80211)
OBJS += $(DRIVER_NL80211)/driver_nl80211.o
endif
#end of driver

#################################################
# protocol
#################################################
ifeq ($(CONFIG_IEEE80211AC), y)
CFLAGS += -DCONFIG_IEEE80211AC
endif

ifeq ($(CONFIG_TDLS), y)
CFLAGS += -DCONFIG_TDLS
endif
# end of protocol

#################################################
# misc
#################################################
ifeq ($(CONFIG_NO_STDOUT_DEBUG), y)
CFLAGS += -DCONFIG_NO_STDOUT_DEBUG
endif
#end of misc

$(OBJS) $(OBJS_cli) : defconfig

%.o: %.c
	@echo ' CC ' $@
	@$(CC) $(CFLAGS) -c -o $@ $<

wfs: $(OBJS) Makefile
	@echo ' CC ' wfs
	@$(CC) $(LDFLAGS) $(OBJS) $(LIBS) -o wfs

wfs_cli: $(OBJS_cli) Makefile
	@echo ' CC ' wfs_cli
	@$(CC) -o wfs_cli $(OBJS_cli)

.PHONY: clean

clean:
	@$(MAKE) -C $(SRC) clean > /dev/null
	@rm -f *~ *.o .*.swp .*.swo $(ALL)
